package com.example.zce.back.controller.common;

import com.example.zce.back.common.oss.AliOSS;
import com.example.zce.library.common.bean.ResultMapBean;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：公共访问
 *
 * @author zengyz
 * @date 2018/7/24 20:39
 */
@Controller
public class CommonController {

    @Autowired
    private AliOSS aliOSS;

    /**
     * 登录页面
     *
     * @param request
     * @return
     */
    @GetMapping("/login.html")
    public ModelAndView loginHtml(@ModelAttribute("msg") String msg, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("login");
        if (StringUtils.isNotEmpty(msg)){
            modelAndView.addObject("error",msg);
        }
        return modelAndView;
    }

    /**
     * 登录验证-shiro验证完之后这里进行异常捕获
     * @param request
     * @param redirectAttributes
     * @return
     */
    @PostMapping("/login.html")
    public ModelAndView loginSubmit(HttpServletRequest request, RedirectAttributes redirectAttributes) {
        String exception = (String) request.getAttribute("shiroLoginFailure");
        String msg = "";
        if (exception != null) {
            if (UnknownAccountException.class.getName().equals(exception)) {
                msg = "账号不存在";
            } else if (IncorrectCredentialsException.class.getName().equals(exception)) {
                msg = "密码错误";
            } else if (DisabledAccountException.class.getName().equals(exception)) {
                msg = "账户冻结，禁止登录";
            } else {
                msg = "登录失败，未知异常，请联系管理员";
            }
            redirectAttributes.addFlashAttribute("msg",msg);
            return new ModelAndView("redirect:/login.html");
        }else{
            return null;
        }
    }

    @ResponseBody
    @PostMapping("/upload.json")
    public ResultMapBean uploadImg(MultipartHttpServletRequest request) {
        List<MultipartFile> file = request.getMultiFileMap().get("file");
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.get(0).getBytes();
                String imgAddress = aliOSS.uploadOSS("IMG", bytes);
                return ResultMapBean.ok(imgAddress);
            } catch (IOException e) {
                return ResultMapBean.error(500,"上传失败");
            }
        }else{
            return ResultMapBean.error(500,"上传失败");
        }
    }

    @ResponseBody
    @PostMapping("/uploadLayedit.json")
    public Map<String, Object> uploadLayeditImg(MultipartHttpServletRequest request) {
        List<MultipartFile> file = request.getMultiFileMap().get("file");
        Map result=new HashMap();
        Map resultData=new HashMap();
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.get(0).getBytes();
                String imgAddress = aliOSS.uploadOSS("IMG", bytes);
                result.put("code","0");
                result.put("msg","SUCCESS");
                result.put("data",resultData);
                resultData.put("src",imgAddress);
                resultData.put("title","");
                return result;
            } catch (IOException e) {
                return ResultMapBean.error(500,"上传失败");
            }
        }else{
            return ResultMapBean.error(500,"上传失败");
        }
    }

}
